package net.lab1024.sa.admin.module.business.orders.service;

import java.util.List;
import net.lab1024.sa.admin.module.business.orders.dao.RetailOrdersDao;
import net.lab1024.sa.admin.module.business.orders.domain.entity.RetailOrdersEntity;
import net.lab1024.sa.admin.module.business.orders.domain.form.RetailOrdersAddForm;
import net.lab1024.sa.admin.module.business.orders.domain.form.RetailOrdersQueryForm;
import net.lab1024.sa.admin.module.business.orders.domain.form.RetailOrdersUpdateForm;
import net.lab1024.sa.admin.module.business.orders.domain.vo.RetailOrdersVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 零售订单 Service
 *
 * @Author 谭智得
 * @Date 2025-03-19 09:46:50
 * @Copyright 谭智得
 */

@Service
public class RetailOrdersService {

    @Resource
    private RetailOrdersDao retailOrdersDao;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<RetailOrdersVO> queryPage(RetailOrdersQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<RetailOrdersVO> list = retailOrdersDao.queryPage(page, queryForm);
        PageResult<RetailOrdersVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(RetailOrdersAddForm addForm) {
        RetailOrdersEntity retailOrdersEntity = SmartBeanUtil.copy(addForm, RetailOrdersEntity.class);
        retailOrdersDao.insert(retailOrdersEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(RetailOrdersUpdateForm updateForm) {
        RetailOrdersEntity retailOrdersEntity = SmartBeanUtil.copy(updateForm, RetailOrdersEntity.class);
        retailOrdersDao.updateById(retailOrdersEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        retailOrdersDao.batchUpdateDeleted(idList, true);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer retailId) {
        if (null == retailId){
            return ResponseDTO.ok();
        }

        retailOrdersDao.updateDeleted(retailId,true);
        return ResponseDTO.ok();
    }
}
